package WebServices
{
	import Config.SkillsAuditConfig;
	
	import flash.errors.IllegalOperationError;
	
	/////////////////////////////////////////////////////////////////////////////////
	// Singleton class exposing Web Service functionality
	/////////////////////////////////////////////////////////////////////////////////
	public class WebServiceFacade
	{
		//
		// Property allowing access to the singleton instance
		//
		static public function get instance():WebServiceFacade
		{
			return theFacade;						
		}
		
		static private var theFacade:WebServiceFacade = new WebServiceFacade(null);

		// Pointers to calling class's callback functions
		private var m_fnSuccess:Function;
		private var m_fnFailure:Function;
		private const m_strLoginPath:String = "skillsAudit.php";
		private const m_strAcaListDocumentPath:String = "skillsAudit.php";
		private const m_strFetchDocumentPath:String = "skillsAudit.php";
		private const m_strUpdateDocumentPath:String = "skillsAudit.php";
		private const m_strUnlockDocumentPath:String = "skillsAudit.php";
		private const m_strLogoutPath:String = "skillsAudit.php";
		
		private var m_loginService:SkillsAuditService = new SkillsAuditService();
		private var m_fetchDocumentService:SkillsAuditService = new SkillsAuditService();
		private var m_updateDocumentService:SkillsAuditService = new SkillsAuditService();
		private var m_unlockDocumentService:SkillsAuditService = new SkillsAuditService();
		private var m_acaList:SkillsAuditService = new SkillsAuditService();
		private var m_logoutService:SkillsAuditService = new SkillsAuditService();
		private var auth:AuthenticationData = new AuthenticationData();

		public function WebServiceFacade(doNotConstructThis:Object)
		{
			if ( theFacade != null )  
         	{  
  				throw new IllegalOperationError("Cannot instantiate new WebServiceFacade. Use WebServiceFacade.instance.");
			}

			m_loginService.url = SkillsAuditConfig.webServicesRoot + m_strLoginPath;
			m_fetchDocumentService.url = SkillsAuditConfig.webServicesRoot + m_strFetchDocumentPath;
			m_acaList.url = SkillsAuditConfig.webServicesRoot + m_strAcaListDocumentPath;
			m_updateDocumentService.url = SkillsAuditConfig.webServicesRoot + m_strUpdateDocumentPath;
			m_unlockDocumentService.url = SkillsAuditConfig.webServicesRoot + m_strUnlockDocumentPath;
			m_logoutService.url = SkillsAuditConfig.webServicesRoot + m_strLogoutPath;
		}

		// 
		// fetchDocume6nt: retrieves the data for the document with document ID == docID into the global 
		// 		document object.
		// docId: id number of the document to be retrieved
		// success: callback function. Should be 'function success():void' and is called if the document 
		//          is updated successfully. 
		// failure: callback function. Should be 'function failure(err:Error):void' and is called if the 
		//          document cannot be updated successfully. The document state is undefined in this case. 
		public function fetchDocument(docId:String, success:Function, failure:Function ):void
		{
			var responder:FetchDocumentResponder = new FetchDocumentResponder(auth, success, failure);
			responder.callServer(m_fetchDocumentService, docId);			
		}
		
		// 
		// updateDocument: sends the current contents of the global document object to the server to be
		//          updated in the database
		// success: callback function. Should be 'function success():void' and is called if the document 
		//          is updated successfully. 
		// failure: callback function. Should be 'function failure(err:Error):void' and is called if the 
		//          document object cannot be updated successfully. The document state is undefined in this case. 
		public function updateDocument(success:Function, failure:Function ):void
		{
			var responder:UpdateDocumentResponder = new UpdateDocumentResponder(auth, success, failure);
			responder.callServer(m_updateDocumentService);			
		}
		
		// 
		// unlockDocument: sends the current contents of the global document object to the server to be
		//          updated in the database
		// success: callback function. Should be 'function success():void' and is called if the document 
		//          is updated successfully. 
		// failure: callback function. Should be 'function failure(err:Error):void' and is called if the 
		//          document object cannot be updated successfully. The document state is undefined in this case. 
		public function unlockDocument(docId:String, success:Function, failure:Function ):void
		{
			var responder:UnlockDocumentResponder = new UnlockDocumentResponder(auth, success, failure);
			responder.callServer(m_unlockDocumentService, docId);
		}
		
		//For Auto List
		public function autoList(success:Function, failure:Function, aString:String, index:int, tableName:String, rowName:String, dataFieldName:String, dataName:String, which:String):void
		{
			var responder:AutoCompleteWebService = new AutoCompleteWebService(auth, success, failure);
			responder.callServer(m_acaList, aString, index, tableName, rowName, dataFieldName, dataName, which);
		}

		// 
		// updateDocument: sends the current contents of the global document object to the server to be
		//          updated in the database
		// success: callback function. Should be 'function success(roles:Array):void' and is called if the
		//          document is updated successfully. When called roles will contain all roles assigned to
		//          the logged in user. 
		// failure: callback function. Should be 'function failure(err:Error):void' and is called if the 
		//          document cannot be updated successfullyin the database. 
		public function login(username:String, password:String, success:Function, failure:Function ):void
		{
			var responder:LoginResponder = new LoginResponder(auth, success, failure);
			responder.callServer(m_loginService, username, password);						
		}		

		// 
		// logout:  disconnects the current user
		// success: callback function. Should be 'function success():void' and is called if the
		//          user is logged out successfully. 
		// failure: callback function. Should be 'function failure(err:Error):void' and is called if the 
		//          user cannot be logged out successfully. 
		public function logout(success:Function, failure:Function ):void
		{
			auth.username = "";
			auth.password = "";
			var responder:LogoutResponder = new LogoutResponder(auth, success, failure);
			responder.callServer(m_logoutService);						
		}
		
		public function getCurrentUser():String
		{
			return auth.username;
		}
	}
}